<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/8 0008
 * Time: 上午 10:04
 */
namespace app\admin\controller;

use think\Request;

class Setapi extends Base{
    public function main(){
        $info = db('apptoken')->where('id',1)->find();
        $info['secrect'] = substr($info['secrect'], 0, 5).'******'.substr($info['secrect'], 15);
        return $this->fetch('',[
            'info' => $info
        ]);
    }

    public function showsecrect(){
        $token = db('apptoken')->where('id',1)->field('phone')->find();
        $token['phone'] = substr($token['phone'], 0, 3).'****'.substr($token['phone'], 7);
        return $this->fetch('',[
            'info' => $token
        ]);
    }

    public function doshowtoken(Request $request){
        $param = $request->param();
        if(!empty($param['api_phone']) || !empty($param['phone_code'])){
            $token = db('apptoken')->where('id',1)->find();
            if($token){
                if(trim($param['api_phone']) != $token['phone']){
                    return $this->error('手机号错误');
                }
            }else{
                return $this->error('系统异常');
            }
        }else{
            return $this->error('参数错误');
        }
    }

    public function editsecrect(){
        $token = db('apptoken')->where('id',1)->field('phone')->find();
        $token['phone'] = substr($token['phone'], 0, 3).'****'.substr($token['phone'], 7);
        return $this->fetch('',[
            'info' => $token
        ]);
    }

    public function doeditsecrect(Request $request){
        if($request->isPost()){
            $param = $request->param();
            if(!empty($param['secrect'])){
                db('apptoken')->where('id',1)->update(['secrect'=>$param['secrect']]);
                return $this->success('ok');
            }else{
                return $this->error('参数错误');
            }
        }else{
            return $this->error('非法请求');
        }
    }
}